package io.hxci.modules.tea.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.hxci.atest.ExcelUtils;
import io.hxci.common.annotation.SysLog;
import io.hxci.common.utils.PageUtils;
import io.hxci.common.utils.R;
import io.hxci.modules.sys.controller.AbstractController;
import io.hxci.modules.tea.domain.TeaDict;
import io.hxci.modules.tea.domain.TeaOther;
import io.hxci.modules.tea.service.TeaDictService;
import io.hxci.modules.tea.service.TeaOtherService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("tea/other")
public class TeaOtherController extends AbstractController {
    @Autowired
    private TeaOtherService teaOtherService;
    @Autowired
    private TeaDictService teaDictService;

    @SysLog("导出教职工其他信息")
    @RequestMapping("/exportExcel")
    public void exportExcel(@RequestParam Map<String, Object> params)  throws IOException {

        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();

        List<TeaOther> resultList = teaOtherService.exportExcel(params);
        ExcelUtils.writeExcel("教职工其他信息.xlsx",response, resultList, TeaOther.class);
    }


    @SysLog("教职工其他信息情况")
    @RequestMapping("/query")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = teaOtherService.queryPage(params);
        return R.ok().put("page", page);
    }

    @SysLog("更新教职工其他信息")
    @RequestMapping("saveOrUpdate")
    public R save(@RequestBody TeaOther teaOther){
//        if(teaOther.getUserId() == null){
//            teaOther.setUserId(getUserId());
//        }
        teaOtherService.saveOrUpdate(teaOther);
        return R.ok();
    }

    @SysLog("删除教职工其他信息")
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] id){
        teaOtherService.removeByIds(Arrays.asList(id));
        return R.ok();
    }

    @SysLog("编辑教职工其他信息")
    @RequestMapping("/edit/{id}/{type}")
    @ResponseBody
    public R edit(@PathVariable("id") Integer id,@PathVariable("type") String type){
        TeaOther edit = teaOtherService.getById(id);
        List<TeaDict> list = teaDictService.list(new QueryWrapper<TeaDict>().like(StringUtils.isNotBlank(type),"type", type));
        return R.ok().put("edit", edit).put("kdoptions",list);
    }

}
